package com.guli.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.eduservice.bean.EduCourse;
import com.guli.eduservice.bean.EduCourseDescription;
import com.guli.eduservice.bean.vo.CourseInfoVo;
import com.guli.eduservice.bean.vo.CoursePublishVo;
import com.guli.eduservice.bean.vo.front.CourseQueryVo;
import com.guli.eduservice.bean.vo.front.CourseWebVo;
import com.guli.eduservice.mapper.EduCourseMapper;
import com.guli.eduservice.service.EduCourseDescriptionService;
import com.guli.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-05
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Resource
    private EduCourseDescriptionService eduCourseDescriptionService;

    //添加课程基本信息
    @Override
    public String saveEduCourse(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean save = false;
        try {
            save = this.save(eduCourse);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new GuliException(20001,"添加课程信息失败");
        }
        if(!save){
            throw  new GuliException(20001,"添加课程信息失败");
        }
        String id = eduCourse.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //必须设置相同id，（更改主键策略为INPUT）
        eduCourseDescription.setId(id);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        boolean save1 = eduCourseDescriptionService.save(eduCourseDescription);
        if(!save1){
            throw new GuliException(20001,"添加课程简介失败");
        }
        return id;
    }

    //根据课程id查询基本信息
    @Override
    public CourseInfoVo getEduCourse(String id) {
        EduCourse eduCourse = this.getById(id);
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        CourseInfoVo courseInfoVo = new CourseInfoVo();

        if(eduCourse != null){
            BeanUtils.copyProperties(eduCourse,courseInfoVo);
        }
        if(eduCourseDescription != null){
            courseInfoVo.setDescription(eduCourseDescription.getDescription());
        }
        return courseInfoVo;
    }

    @Override
    public void updateEduCourse(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean update = false;
        try {
            update = this.updateById(eduCourse);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new GuliException(20001,"修改课程信息失败");
        }
        if(!update){
            throw  new GuliException(20001,"修改课程信息失败");
        }
        String id = eduCourse.getId();
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //必须设置相同id，（更改主键策略为INPUT）
        eduCourseDescription.setId(id);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        boolean update1 = eduCourseDescriptionService.updateById(eduCourseDescription);
        System.out.println(update1);
        if(!update1){
            throw new GuliException(20001,"修改课程简介失败");
        }
    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        return baseMapper.selectCoursePublishVoById(id);
    }

  //  @Cacheable(value = "course",key = "'getEightCourse'")
    @Override
    public List<EduCourse> getEightCourse() {
        //查询前8条热门课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id").last("limit 8");
        List<EduCourse> courseList = baseMapper.selectList(wrapper);
        return courseList;
    }

    //分页显示课程列表
    @Override
    public Map<String, Object> getCoursePage(Page<EduCourse> pageParam, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if(courseQuery!=null){
            if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
                queryWrapper.eq("subject_parent_id",
                        courseQuery.getSubjectParentId());
            }
            if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
                queryWrapper.eq("subject_id", courseQuery.getSubjectId());
            }
            if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
                queryWrapper.orderByDesc("buy_count");
            }
            if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
                queryWrapper.orderByDesc("gmt_create");
            }
            if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
                queryWrapper.orderByDesc("price");
            }
        }

        baseMapper.selectPage(pageParam,queryWrapper);
        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo selectInfoWebById(String id) {
        this.updatePageViewCount(id);
        return baseMapper.selectCourseWebVoById(id);
    }

    @Override
    public void updatePageViewCount(String id) {
        EduCourse eduCourse = baseMapper.selectById(id);
        eduCourse.setViewCount(eduCourse.getViewCount() + 1);
        baseMapper.updateById(eduCourse);
    }
}
